/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include "stdio.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "securec.h"

#include "iot_gpio.h"
#include "iot_adc.h"
#include "iot_errno.h"
#include "sensor.h"
#include "beep_demo.h"

//初始化蜂鸣器相关参数
struct BEEPCTRLATTR gBeepCtrlAttr = {
    .times = 0,
    .duty  = 0.4,
    .period = 500,
    .callTime = 200 
};

/**
 * @brief 处理ADC 电压变化
 * @param [in] data 电压值
 * @param [in] len 长度
 * @return 
 */
static void OnAdcData(uint16_t *data, uint32_t len)
{
    float vlt_max = 0;   //最大值
    float vlt_min = 100; //最小值
    for (size_t i = 0; i < len; i++)
    {
        float voltage = (float)data[i] * 1.8 * 4 / 4096.0; /* vlt * 1.8 * 4 / 4096.0: Convert code into voltage */
        vlt_max = (voltage > vlt_max) ? voltage : vlt_max;
        vlt_min = (voltage < vlt_min) ? voltage : vlt_min;
    }

    float vlt_val = (vlt_min + vlt_max) / 2.0; //平均值
    printf("MQ-2 Voltage = %.3f \r\n", vlt_val);

    if (vlt_val >= 1.0f)
    { 
        //电压变化超值1.0f
        gBeepCtrlAttr.times = 2;
        BeepCtrl(&gBeepCtrlAttr);
    }
    else 
    {
        gBeepCtrlAttr.times = 0;
        BeepCtrl(&gBeepCtrlAttr);
    }
}

static void *CGSensorTask(const char *arg)
{
    (void)arg;

    uint16_t th_adc_buf[ADC_TEST_LENGTH] = {0};

    while (1)
    {
        uint32_t ret;
        memset_s(th_adc_buf, sizeof(th_adc_buf), 0x0, sizeof(th_adc_buf));

        for (size_t i = 0; i < ADC_TEST_LENGTH; i++)
        {
            ret = IoTAdcRead(IOT_ADC_CHANNEL_5, &th_adc_buf[i], IOT_ADC_EQU_MODEL_1, IOT_ADC_CUR_BAIS_DEFAULT, 0);
            if (ret != IOT_SUCCESS)
            {
                printf("ADC Read Fail\n");
                return;
            }
        }
        OnAdcData(th_adc_buf, ADC_TEST_LENGTH);
        // 延迟500ms
        osDelay(50);
    }                  
}

/**
 * @brief 烟感传感器服务
 * @return 
 */
static void CGsensorExampleEntry(void)
{
    osThreadAttr_t attr;

    BeepInit();
    // ADC初始化
    IoTGpioInit(IOT_GPIO_IO_GPIO_11);
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_11, IOT_IO_FUNC_GPIO_11_GPIO);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_11, IOT_GPIO_DIR_IN);

    attr.name = "CHSensorTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = CGSENSOR_TASK_STACK_SIZE;
    attr.priority = CGSENSOR_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)CGSensorTask, NULL, &attr) == NULL)
    {
        printf("[CGsensorExampleEntry] Falied to create CGSensorTask!\n");
    }
}

APP_FEATURE_INIT(CGsensorExampleEntry);
